package com.tuanzi.loan.business.repository.extratemplate.impl;

import com.tuanzi.loan.business.repository.extratemplate.custom.ExtraTemplateRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.generated.jooq.loan.tables.LoanExtraTemplate;
import com.tuanzi.loan.vo.request.extratemplate.ExtraTemplateRequest;
import com.tuanzi.loan.vo.response.extratemplate.ExtraTemplateResponse;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanExtraTemplate.LOAN_EXTRA_TEMPLATE;

/**
 * @author caiwanlin
 * @create 2018-10-16 14:21
 */
public class ExtraTemplateRepositoryImpl extends JOOQBaseRepositoryPlus implements ExtraTemplateRepositoryCustom {
    @Override
    public Page<ExtraTemplateResponse> list(ExtraTemplateRequest searchRequest, Pageable pageable) {
        Integer templateId = searchRequest.getTemplateId();
        String templateName = searchRequest.getTemplateName();
        fields(LOAN_EXTRA_TEMPLATE.ID,LOAN_EXTRA_TEMPLATE.TEMPLATE_ID,LOAN_EXTRA_TEMPLATE.TEMPLATE_NAME,
                LOAN_EXTRA_TEMPLATE.CREATOR,LOAN_EXTRA_TEMPLATE.CREATE_TIME,LOAN_EXTRA_TEMPLATE.UPDATOR,
                LOAN_EXTRA_TEMPLATE.UPDATE_TIME);
        eq(LOAN_EXTRA_TEMPLATE.TEMPLATE_ID,templateId);
        contains(LOAN_EXTRA_TEMPLATE.TEMPLATE_NAME,templateName);
        orderBy(LOAN_EXTRA_TEMPLATE.CREATE_TIME, Sort.Direction.DESC);

        Page<ExtraTemplateResponse> search = search(pageable, record -> {
            ExtraTemplateResponse response = new ExtraTemplateResponse();
            response.setId(record.get(LOAN_EXTRA_TEMPLATE.ID));
            response.setTemplateId(record.get(LOAN_EXTRA_TEMPLATE.TEMPLATE_ID));
            response.setTemplateName(record.get(LOAN_EXTRA_TEMPLATE.TEMPLATE_NAME));
            response.setCreator(record.get(LOAN_EXTRA_TEMPLATE.CREATOR));
            response.setCreateTime(record.get(LOAN_EXTRA_TEMPLATE.CREATE_TIME));
            response.setUpdator(record.get(LOAN_EXTRA_TEMPLATE.UPDATOR));
            response.setUpdateTime(record.get(LOAN_EXTRA_TEMPLATE.UPDATE_TIME));
            return response;
        });
        return search;
    }

    @Override
    protected TableImpl getTable() {
        return LOAN_EXTRA_TEMPLATE;
    }
}
